package com.stylefeng.guns.modular.system.api;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.response.JsonResult;
import com.stylefeng.guns.core.response.ResultCode;
import com.stylefeng.guns.core.response.ResultGenerator;
import com.stylefeng.guns.core.util.IpUtil;
import com.stylefeng.guns.modular.system.model.Jarset;
import com.stylefeng.guns.modular.system.service.IJarsetService;
import com.stylefeng.guns.modular.system.service.IJaruseService;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/api")
public class JarsetApiController extends BaseController {

    @Autowired
    IJaruseService iJaruseService;

    @Autowired
    IJarsetService iJarsetService;

    //返回jar包反射参数列表
    @RequestMapping(value = "/jarset")
    public JsonResult jarInformation(HttpServletRequest request){
        String channel = request.getParameter("channel");
        String jarName = request.getParameter("jarName");
        List<Jarset> list = iJarsetService.showInfoByChannelAndJarName(channel,jarName);
        Jarset jarset = null;
        if(list!=null&&list.size()>0) {
            jarset = list.get(0);//需要的条数-
            Integer count = iJaruseService.selByChannelAndJarName(channel, jarName);//总共有这么多条
            //进行控量
            if(count>jarset.getUserNumber()){
                jarset.setIsUse(0);//不可以使用
                iJarsetService.updIsUse(channel,jarName);//修改isuse=1
            }else{
                //可以继续使用，默认isuse=1
                return ResultGenerator.genSuccessResult(jarset);
            }
        }else{
            jarset = new Jarset();
            jarset.setChannel(channel);
            jarset.setJarName(jarName);
            jarset.setIsUse(0);//不可以使用
        }
        return ResultGenerator.genSuccessResult(jarset);
    }
    /**
     * 根据渠道ID返回广告主jar包列表和子渠道数
     * */
    @RequestMapping(value = "/getJarInfoByChannelName/{channelName}")
    public JsonResult getJarInfoByChannelName(@PathVariable String channelName){
        Map<Object,Object> map = new HashMap<Object, Object>();
        List<Jarset> jarsetList = iJarsetService.showInfoByChannel(channelName);
        if(jarsetList.size()==0){
            return ResultGenerator.genFailResult("wrong", ResultCode.CHANNEL_NOT_FOUND);
        }
        map.put("jarsetList",jarsetList);
        map.put("channelSonNumber",jarsetList.size());
        return ResultGenerator.genSuccessResult(map);
    }

    //返回包的使用情况
    @RequestMapping(value = "/jarsetByParam")
    public JsonResult jarInfo(HttpServletRequest request){
        String channel = request.getParameter("channel");//渠道
        String jarName = request.getParameter("jarName");//jar包名称
        String ip = request.getParameter("ip");//ip地址
        String model = request.getParameter("model");//手机牌子
        String operator = request.getParameter("operator");//运营商
        String country = IpUtil.address(ip,0);//获取国家
        List<Jarset> list = iJarsetService.showInfoByChannelAndJarName(channel,jarName);
        Jarset jarset = null;
        if(list!=null&&list.size()>0) {
            jarset = list.get(0);//需要的条数
            Integer count = iJaruseService.selByChannelAndJarName(channel, jarName);//总共有这么多条
            //进行控量
            if(count>jarset.getUserNumber()){
                jarset.setIsUse(0);//不可以使用
                iJarsetService.updIsUse(channel,jarName);//修改isuse=1
            }else if(model.equals(jarset.getModel())) {
                //控制手机牌子
            }else if(operator.equals(jarset.getOpeartor())){
                //控制运营商
            }else if(country.equals(jarset.getCountry())){
                //控制国家
            }else{
                //可以继续使用，默认isuse=1
                return ResultGenerator.genSuccessResult(jarset);
            }
        }else{
            jarset = new Jarset();
            jarset.setChannel(channel);
            jarset.setJarName(jarName);
            jarset.setIsUse(0);//不可以使用
        }
        return ResultGenerator.genSuccessResult(jarset);
    }

}
